﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Lapas.Common.Forms
{
    public partial class ConfirmForm : BaseForm
    {
        private string titleField;

        public string Title
        {
            get { return titleField; }
            set { titleField = value; }
        }

        private string descriptionField;

        public string Description
        {
            get { return descriptionField; }
            set { descriptionField = value; }
        }

        private ConfirmFormResult resultField;

        public ConfirmFormResult Result
        {
            get { return resultField; }
            set { resultField = value; }
        }


        public ConfirmForm()
        {
            InitializeComponent();
            this.GenerateCodeType = GenerateConfirmCodeType.Len8;
        }

        public GenerateConfirmCodeType GenerateCodeType { get; set; }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (confirmCodeTextBox.Text.ToUpper() == confirmCodeLabel.Text.ToUpper())
            {
                resultField = ConfirmFormResult.ValidConfirm;
                this.Close();
            }
            else
            {
                RaiseError(confirmCodeTextBox, "Mã xác nhận không hợp lệ. Hãy nhập lại mã khác", true);
                GenerateConfirmCode();
                resultField = ConfirmFormResult.InvalidConfirm;
            }
        }

        private void ConfirmForm_Load(object sender, EventArgs e)
        {
            header1.Title = titleField;
            header1.Description = descriptionField;
            GenerateConfirmCode();
            this.Text = titleField;
            confirmCodeTextBox = (TextBox)UIFormat.Control_Format(confirmCodeTextBox);
        }

        private void GenerateConfirmCode()
        {
            switch (GenerateCodeType)
            {
                case GenerateConfirmCodeType.Guid:
                    confirmCodeLabel.Text = Guid.NewGuid().ToString().ToUpper();
                    break;
                case GenerateConfirmCodeType.Len6:
                    confirmCodeLabel.Text = Helper.Generate(3);
                    break;
                case GenerateConfirmCodeType.Len8:
                    confirmCodeLabel.Text = Helper.Generate(4);
                    break;
                default:
                    confirmCodeLabel.Text = Guid.NewGuid().ToString().ToUpper();
                    break;
            }            
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            resultField = ConfirmFormResult.InvalidConfirm;
            this.Close();
        }

        private void GenerateButton_Click(object sender, EventArgs e)
        {
            this.GenerateConfirmCode();
        }
    }
}